package com.yyj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyj.dao.DeptDao;
import com.yyj.entity.Dept;
import com.yyj.entity.DeptParams;
import com.yyj.exception.CustomException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptService {

    final
    DeptDao deptDao;

    public DeptService(DeptDao deptDao) {
        this.deptDao = deptDao;
    }

    public List<Dept> getDept() {
        return deptDao.getDept();
    }

    public PageInfo<Dept> getBySearch(DeptParams deptParams) {
        //开启分页查询
        PageHelper.startPage(deptParams.getPageNum(), deptParams.getPageSize());
        //裸数据 不包括数据总数,当前页和页面大小
        List<Dept> list = deptDao.getBySearch(deptParams);
        //封装在一起的数据
        return PageInfo.of(list);
    }

    public void insertDept(Dept dept) {
        if (dept.getName() == null || dept.getName().isEmpty()) {
            throw new CustomException("部名不能为空");
        }
        //添加管理员的重复性判断
        Dept addedDept = deptDao.selectByName(dept.getName());
        if (addedDept == null) {
            deptDao.insertDept(dept);
        } else {
            //已经有了，提示前台
            throw new CustomException("该部已经存在，请勿重复添加");
        }


    }

    public void updateDept(Dept dept) {
        deptDao.updateDept(dept);
    }


    public void delete(Integer id) {
        deptDao.deleteByPrimaryKey(id);
    }


}
